#!/bin/bash

# Copyright (c) 2022 FuTeng
# FuYao-RepoPackageTest-System is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#          http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.

# #################################################
# @Author    :   futeng
# @Contact   :   lanshaa123@163.com
# @Desc      :   单个机器人执行的软件包安装卸载测试脚本
# @Note      :   宿主机需要安装docker
# #################################################

source ./parameters/global-variable.sh
source ./parameters/functions.sh

for pkgROBOTNUM in `cat $PACKAGE_LIST_DIR/repoPkgs-ROBOTNUM.list`
do
	date +%Y-%m-%d-%H-%M-%S
	if [ -d $TEST_TEMP/robot-ROBOTNUM ]
	then
		# 目录存在，不必创建
        echo " " > /dev/null
	else
		mkdir $TEST_TEMP/robot-ROBOTNUM
	fi
	echo "现在执行软件包 $pkgROBOTNUM 的安装卸载测试" 
	IMAGE_ID=$(docker images | grep $I_UUID | awk '{print $3}')
	echo "被测镜像的ID是$IMAGE_ID"

	echo "生成包含被测软件包包名的文件"
	echo "$pkgROBOTNUM" > $TEST_TEMP/robot-ROBOTNUM/robot-pkg

	echo "拷贝测试脚本到机器人（容器）对应测试目录"
	cp -p $TEST_TEMP/pkg-test.sh $TEST_TEMP/robot-ROBOTNUM/

	echo "机器人执行软件包 $pkgROBOTNUM 安装卸载测试"
	docker run --name PKGTEST-ROBOTNUM-$I_UUID -v $TEST_TEMP/robot-ROBOTNUM/:/repotest $IMAGE_ID /bin/bash /repotest/pkg-test.sh
	CONTAINER_ID=$(docker ps -a | grep PKGTEST-ROBOTNUM-$I_UUID | awk '{print $1}')

	echo "删除机器人（容器）$CONTAINER_ID"
    docker rm $CONTAINER_ID
	date +%Y-%m-%d-%H-%M-%S
	echo "#######################################################################################################################################"
	echo ""
done